﻿using Kiaser.Core.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kiaser.Core
{
   public class AdministratorManager:BaseManager<Administrator>
    {
       /// <summary>
       /// 添加
       /// </summary>
       /// <param name="admin">管理员实体</param>
       /// <returns></returns>
       public override Response Add(Administrator admin)
       {
           Response _response = new Response();
           if (HasAccounts(admin.Accounts))
           {
               _response.Code = 0;
               _response.Message = "账号已存在";
           }
           else
           {
               _response = base.Add(admin);
           }

           return _response;
       }

       /// <summary>
       /// 修改密码
       /// </summary>
       /// <param name="administratorID">主键</param>
       /// <param name="password">密码【密文】</param>
       /// <returns></returns>
       public Response ChangePassword(int administratorID,string password)
       {
           Response _reponse = new Response();
           var _admin = base.Find(administratorID);
           if (_admin == null)
           {
               _reponse.Code = 0;
               _reponse.Message = "该主键管理员不存在";
           }
           else
           {
               _admin.Password = password;
               _reponse = base.Update(_admin);
           }

           return _reponse;
       }

       /// <summary>
       /// 删除
       /// </summary>
       /// <param name="administratorID">管理员主键</param>
       /// <returns></returns>
       public Response Delete(int administratorID)
       {
           Response _response = new Response();

           if (Count() == 1)
           {
               _response.Code = 0;
               _response.Message = "不能删除唯一的管理员账号";
           }
           else
           {
               _response = Delete(administratorID);
           }
           return _response;
       }

       /// <summary>
       /// 查找
       /// </summary>
       /// <param name="accounts">账号</param>
       /// <returns></returns>
       public Administrator Find(string accounts)
       {
           return base.Repository.Find(a => a.Accounts == accounts);
       }

       /// <summary>
       /// 账号是否存在
       /// </summary>
       /// <param name="accounts">账号</param>
       /// <returns></returns>
       public bool HasAccounts(string accounts)
       {
           return base.Repository.IsContains(a => a.Accounts.ToUpper() == accounts.ToUpper());
       }

       /// <summary>
       /// 更新登录信息
       /// </summary>
       /// <param name="administratorID">主键</param>
       /// <param name="ip">ip地址</param>
       /// <param name="time">时间</param>
       /// <returns></returns>
       public Response UpdateLoginInfo(int administratorID, string ip, DateTime time)
       {
           Response _response = new Response();
           var _admin = Find(administratorID);

           if (_admin == null)
           {
               _response.Code = 0;
               _response.Message = "该主键的管理员不存在";
           }
           else
           {
               _admin.LoginIP = ip;
               _admin.LoginTime = time;
               _response = Update(_admin);
           }

           return _response;
       }

       /// <summary>
       /// 登录验证
       /// </summary>
       /// <param name="accounts">账号</param>
       /// <param name="password">密码【密文】</param>
       /// <returns>1-成功；2-账号不存在；3-密码错误</returns>
       public Response Verify(string accounts, string password)
       {
           Response _response = new Response();
           var _admin = Find(accounts);

           if (_admin == null)
           {
               _response.Code = 2;
               _response.Message = "帐号为:【" + accounts + "】的管理员不存在";
           }
           else if (_admin.Password == password)
           {
               _response.Code = 1;
               _response.Message = "验证通过";
           }
           else
           {
               _response.Code = 3;
               _response.Message = "帐号密码错误";
           }

           return _response;
       }
    }
}
